package com.example.xpvideo.service.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.xpvideo.context.PageContext;
import com.example.xpvideo.entity.LsGoods;
import com.example.xpvideo.entity.PageResult;
import com.example.xpvideo.entity.ResponseResult;
import com.example.xpvideo.entity.req.PHPCommodityReq;
import com.example.xpvideo.entity.req.PhpCommodityAuditReq;
import com.example.xpvideo.entity.req.PhpTopGoodsReq;
import com.example.xpvideo.entity.req.UpdateReq;
import com.example.xpvideo.entity.res.PHPCommodityDetailRes;
import com.example.xpvideo.entity.res.PHPCommodityRes;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.exception.ErrorException;
import com.example.xpvideo.mapper.LsGoodsMapper;
import com.example.xpvideo.mapper.PHPCommodityMapper;
import com.example.xpvideo.service.PhpGoodsService;
import com.example.xpvideo.service.TimeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class PhpGoodsServiceImpl implements PhpGoodsService {
    @Autowired
    private LsGoodsMapper lsGoodsMapper;
    @Autowired
    private PHPCommodityMapper phpCommodityMapper;
    @Autowired
    private TimeService timeService;

    @Override
    public ResponseResult<PageResult<List<PHPCommodityRes>>> list(PHPCommodityReq phpCommodityReq) throws ErrorException {

        int page = PageContext.getPage();
        int size = PageContext.getSize();
        String merchantsName = phpCommodityReq.getShopName();
        String commodityName = phpCommodityReq.getName();
        String oneName = phpCommodityReq.getCommodityOneTypeName();
        String twoName = phpCommodityReq.getCommodityTwoTypeName();
        Integer status = phpCommodityReq.getAuditStatus();
        PageHelper.startPage(page, size);

        List<PHPCommodityRes> list = phpCommodityMapper.PhpCommodityList(Wrappers.<LsGoods>query()
                .like(!ObjectUtils.isEmpty(merchantsName),"ls.name",merchantsName)
                .like(!ObjectUtils.isEmpty(commodityName),"lg.name",commodityName)
                .like(!ObjectUtils.isEmpty(oneName), "lgc.name",oneName)
                .like(!ObjectUtils.isEmpty(twoName), "lgc1.name",twoName)
                .eq(!ObjectUtils.isEmpty(status),"audit_status",status));

        PageInfo pageInfo = new PageInfo<>(list);
        //当前页
        int pageNum = pageInfo.getPageNum();
        //总页数
        int pages = pageInfo.getPages();
        //当前页展示的数量
        int pageSize = pageInfo.getSize();
        //查询总数
        long count = pageInfo.getTotal();
        PageResult<List<PHPCommodityRes>> listPageResult = new PageResult<>(count, pages, pageSize, pageNum, list);

        return ResponseResult.responseResult(0, listPageResult);

    }

    @Override
    public ResponseResult<PHPCommodityDetailRes> detail(Integer goodsId) throws ErrorException {

        PHPCommodityDetailRes phpCommodityDetailRes= phpCommodityMapper
                .phpCommodityDetail(Wrappers.<LsGoods>query()
                        .eq("lg.id",goodsId));


        return ResponseResult.responseResult(0, phpCommodityDetailRes);

    }

    @Override
    public ResponseResult<Object> Audit(PhpCommodityAuditReq phpCommodityAuditReq) throws ErrorException{
        Integer id = phpCommodityAuditReq.getId();
        Integer stage = phpCommodityAuditReq.getStage();
        String remark = phpCommodityAuditReq.getRemark();

        LsGoods lsGoods = new LsGoods();
        lsGoods.setId(id);
        lsGoods.setAuditStatus(String.valueOf(stage));
        lsGoods.setUpdateTime(timeService.getTime());

        if(stage == 2){
            lsGoods.setAuditRemark(remark);
        }
        int res = lsGoodsMapper.updateLsGoods(lsGoods);

        if(res > 0){
            return ResponseResult.responseResult(0,"审核成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20000);
    }

    @Override
    public ResponseResult<Object> OffShelf(UpdateReq updateReq) throws ErrorException{
        Integer id = updateReq.getId();
        Integer stage = updateReq.getStage();


        LsGoods lsGoods = new LsGoods();
        lsGoods.setId(id);
        lsGoods.setStatus(stage);
        lsGoods.setUpdateTime(timeService.getTime());

        int res = lsGoodsMapper.updateLsGoods(lsGoods);

        if(res > 0){
            return ResponseResult.responseResult(0,"上架/下架商品成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20000);
    }
    @Override
    public ResponseResult<Object> top (PhpTopGoodsReq phpTopGoodsReq) throws ErrorException{
        Integer id = phpTopGoodsReq.getId();
        Integer stage = phpTopGoodsReq.getStage();
        Integer topDays = phpTopGoodsReq.getTopDays();

        LsGoods lsGoods = new LsGoods();
        lsGoods.setId(id);
        lsGoods.setIsTop(stage);
        lsGoods.setUpdateTime(timeService.getTime());

        if(stage == 0){
            lsGoods.setTopDays(0);
        }
        if(stage == 1){
            lsGoods.setTopDays(topDays);
        }
        int res = lsGoodsMapper.updateLsGoods(lsGoods);

        if(res > 0){
            return ResponseResult.responseResult(0,"置顶/取消置顶商品成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20000);
    }
}
